home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Diamond Collection / The Diamond Collection (Software Vault)(Digital Impact).ISO / cdr44 / xlib06p1.zip / XTEXT.CPP < prev    next >
C/C++ Source or Header  |  1995-03-15  |  7KB  |  240 lines

  1. #include "xinternl.h"
  2. #include <i86.h>
  3. #include <conio.h>
  4. #include <stdio.h>
  5. #include <stdarg.h>
  6. #include <string.h>
  7.  
  8. #define FONT_8x8  0
  9. #define FONT_8x15 1
  10. #define FONT_USER 2
  11.  
  12. #include "xdefs.h"
  13.  
  14. /*==================================================================
  15. XRECT.CPP contains the basic functions for text manipulation in
  16. mode X.  Since I was unable to get the pointers to the VGA ROM
  17. fonts, I simply saved them to disk and included them in the headers,
  18. tacking a couple extra K onto the files.
  19.  
  20. Also note that text routines as given take ( 3 * height ) OUT
  21. instructions per character-- that adds up fast!
  22.  
  23. These routines were written initially by Themie Gouthas and
  24. company and modified March 1995 by Victor B. Putz.
  25. ===================================================================*/
  26.  
  27.  
  28. /* VARIABLES =========================================================== */
  29.  
  30. BYTE CharHeight = 0;     /* Char height of currently active font        */
  31. BYTE CharWidth = 0;      /* Char width of currently active font         */
  32. BYTE FirstChar = 0;      /* First char in the curr. active font         */
  33.  
  34. BYTE UserCharHeight = 0; /* Char height of currentle regist'd user font */
  35. BYTE UserCharWidth = 0;  /* Char height of currentle regist'd user font */
  36. BYTE UserFirstChar = 0;  /* First char of the curr. regist'd usera font */
  37.  
  38. BYTE * _pbFont = NULL;
  39. BYTE * _pcUserFont = NULL;
  40.  
  41. //BYTE * _pbVGA8x8Font = NULL;
  42. //BYTE * _pbVGA8x14Font = NULL;
  43.  
  44. #include "vga8x8.h"
  45. #include "vga8x14.h"
  46.  
  47. // This is a look up table for the mirror image of a byte eg
  48. // a byte with the value 11001010 has a corresponding byte in the table
  49. // 01010011. This is necessary as the VGA rom font bits are the reverse
  50. // order of what we need for the Mode X. If you know a better-faster way
  51. // TELL ME!
  52.  
  53. BYTE abMirrorTable[] = {
  54.     0,128, 64,192, 32,160, 96,224, 16,144, 80,208, 48,176,112,240,
  55.     8,136, 72,200, 40,168,104,232, 24,152, 88,216, 56,184,120,248,
  56.     4,132, 68,196, 36,164,100,228, 20,148, 84,212, 52,180,116,244,
  57.     12,140, 76,204, 44,172,108,236, 28,156, 92,220, 60,188,124,252,
  58.     2,130, 66,194, 34,162, 98,226, 18,146, 82,210, 50,178,114,242,
  59.     10,138, 74,202, 42,170,106,234, 26,154, 90,218, 58,186,122,250,
  60.     6,134, 70,198, 38,166,102,230, 22,150, 86,214, 54,182,118,246,
  61.     14,142, 78,206, 46,174,110,238, 30,158, 94,222, 62,190,126,254,
  62.     1,129, 65,193, 33,161, 97,225, 17,145, 81,209, 49,177,113,241,
  63.     9,137, 73,201, 41,169,105,233, 25,153, 89,217, 57,185,121,249,
  64.     5,133, 69,197, 37,165,101,229, 21,149, 85,213, 53,181,117,245,
  65.     13,141, 77,205, 45,173,109,237, 29,157, 93,221, 61,189,125,253,
  66.     3,131, 67,195, 35,163, 99,227, 19,147, 83,211, 51,179,115,243,
  67.     11,139, 75,203, 43,171,107,235, 27,155, 91,219, 59,187,123,251,
  68.     7,135, 71,199, 39,167,103,231, 23,151, 87,215, 55,183,119,247,
  69.     15,143, 79,207, 47,175,111,239, 31,159, 95,223, 63,191,127,255
  70. };
  71.  
  72. extern int ScrnLogicalByteWidth;
  73. extern BYTE * pbVGABuffer;
  74.  
  75. BYTE FontDriverActive = 0;
  76.  
  77.  
  78. /* FUNCTIONS =========================================================== */
  79.  
  80. WORD x_text_init(
  81.     void
  82. )
  83. {
  84.  
  85.     FontDriverActive = TRUE;
  86.     _pbFont = _pbVGA8x8Font;
  87.   CharHeight = CharWidth = 8;
  88.   return 0;
  89. }
  90.  
  91. /* Set the font style          */
  92. void x_set_font(
  93.     int FontId
  94. )
  95. {
  96.   switch ( FontId ) {
  97.     case 2 : //user font
  98.         _pbFont = _pcUserFont;
  99.       CharHeight = UserCharHeight;
  100.       CharWidth = UserCharWidth;
  101.       FirstChar = UserFirstChar;
  102.       break;
  103.     case 1 :
  104.         _pbFont = _pbVGA8x14Font;
  105.             CharHeight = 14;
  106.             CharWidth = 8;
  107.             FirstChar = 0;
  108.         break;
  109.         case 0 :
  110.             _pbFont = _pbVGA8x8Font;
  111.             CharHeight = 8;
  112.             CharWidth = 8;
  113.             FirstChar = 0;
  114.         break;
  115.   }
  116. }
  117.  
  118. void x_register_userfont(          /* register a user defined font */
  119.  BYTE * UserFontPtr
  120. )
  121. {
  122.   _pcUserFont = UserFontPtr + 4;
  123.   UserFirstChar = *( UserFontPtr );
  124.   UserCharHeight = *( UserFontPtr + 2 );
  125.   UserCharWidth = *( UserFontPtr + 3 );
  126. }
  127.  
  128.  
  129. unsigned int  x_get_char_width(    /* Get the character width      */
  130.   char ch
  131. )
  132. {
  133.   if ( CharWidth != 0 ) {
  134.     return CharWidth;
  135.   }
  136.   else {
  137.     return *( _pbFont + ( ch - FirstChar + 1 )*( CharHeight + 1 ) - 1 );
  138.   }
  139. }
  140.  
  141.  
  142. unsigned short awTextMask[16] = { 0x0002, 0x0102, 0x0202, 0x0302,
  143.                                  0x0402, 0x0502, 0x0602, 0x0702,
  144.                                  0x0802, 0x0902, 0x0A02, 0x0B02,
  145.                                  0x0C02, 0x0D02, 0x0E02, 0x0F02 };
  146.  
  147. unsigned int  x_char_put(          /* Draw a text character using  */
  148.   char ch,                  /* the currently active font    */
  149.   xScreenCoord_t X,
  150.   xScreenCoord_t Y,
  151.   xPageHandle_t PgOffs,
  152.   xColor_t Color
  153. )
  154. {
  155.     int iScreenInc = ScrnLogicalByteWidth - 3;
  156.     BYTE * pbStart = pbVGABuffer + Y * ScrnLogicalByteWidth + X / 4 + PgOffs;
  157.   int iFirstPlane = X & 3;
  158.   BYTE * pbChar = 0;
  159.   if ( CharWidth == 0 ) {
  160.     pbChar =( BYTE * )_pbFont + ( ch - FirstChar ) * ( CharHeight + 1 );
  161.   }
  162.   else {
  163.     pbChar =( BYTE * )_pbFont + ( ch - FirstChar ) * ( CharHeight );
  164.   }
  165.   int iTemp = 0;
  166.   for ( int i = CharHeight; i > 0; --i ) {
  167.     iTemp = *pbChar++;
  168.     if ( _pbFont != _pcUserFont ) {
  169.       iTemp = abMirrorTable[ iTemp ];
  170.     }
  171.     iTemp <<= iFirstPlane;
  172. //TEST STUFF!
  173.     outpw( SC_INDEX, awTextMask[ iTemp & 0x0f ] );
  174.     *pbStart++ = ( BYTE )Color;
  175.     outpw( SC_INDEX, awTextMask[ ( iTemp & 0xf0 ) >> 4 ] );
  176.     *pbStart++ = ( BYTE )Color;
  177.         outpw( SC_INDEX, awTextMask[ iTemp >> 8 ] );
  178.     *pbStart++ = ( BYTE )Color;
  179.  
  180.     pbStart += iScreenInc;
  181.   }
  182.   return x_get_char_width(ch);
  183. }
  184.  
  185.  
  186. /* the folowing function is from xprintf.c but is included due to its     */
  187. /* close relationship with this module                                    */
  188.  
  189. void x_printf(
  190.     xScreenCoord_t x,
  191.     xScreenCoord_t y,
  192.     xPageHandle_t ScrnOffs,
  193.     xColor_t color,
  194.     char *ln,
  195.     ...
  196. )
  197. {
  198.   char dlin[100],*dl=dlin;
  199.   va_list ap;
  200.  
  201.   va_start(ap,ln);
  202.   vsprintf(dlin,ln,ap);
  203.   va_end(ap);
  204.  
  205.   while(*dl){
  206.       x+=x_char_put(*dl++,x,y,ScrnOffs,color);
  207.   }
  208.  
  209. }
  210.  
  211. extern void x_rect_fill(
  212.     xScreenCoord_t, xScreenCoord_t,
  213.     xScreenCoord_t, xScreenCoord_t,
  214.     xPageHandle_t, xColor_t
  215. );
  216.  
  217. void x_bgprintf(
  218.     xScreenCoord_t x,
  219.     xScreenCoord_t y,
  220.     xPageHandle_t ScrnOffs,
  221.     xColor_t fgcolor,
  222.   xColor_t bgcolor,
  223.     char *ln,
  224.     ...
  225. )
  226. {
  227.   char dlin[100],*dl=dlin;
  228.   va_list ap;
  229.  
  230.   va_start(ap,ln);
  231.   vsprintf(dlin,ln,ap);
  232.   va_end(ap);
  233.  
  234.   while(*dl){
  235.       x_rect_fill(x,y,x+x_get_char_width(*dl),y+CharHeight,ScrnOffs,bgcolor);
  236.       x+=x_char_put(*dl++,x,y,ScrnOffs,fgcolor);
  237.   }
  238. }
  239.  
  240.